package com.zxtech.zzm.zxmonitorsocket.resolver

import com.zxtech.zzm.core.util.DeviceUtil
import com.zxtech.zzm.core.util.LoggerUtil
import com.zxtech.zzm.zxmonitorsocket.SocketPackage
import com.zxtech.zzm.zxmonitorsocket.util.SocketLoggerUtil
import java.util.LinkedList
import java.util.Queue

open class SocketBaseResolver internal constructor() {
  companion object {
    private var instance: SocketBaseResolver? = null
      get() {
        if (field == null) {
          field = SocketBaseResolver()
        }
        return field
      }

    fun getIns(): SocketBaseResolver {
      //这里不用getInstance作为为方法名，是因为在伴生对象声明时，内部已有getInstance方法，所以只能取其他名字
      return instance!!
    }
  }

  var packageList: Queue<SocketPackage> = LinkedList()
  open fun resolve(content: Any) {
    val responseData = content as String
    SocketLoggerUtil.logSocket("读取到的数据：$responseData") // 打印日志
    val length = responseData.length
    var flag = 0 //记录解析到的位置

    while (flag < length) {
      flag = responseData.toUpperCase()
          .indexOf("434C5443", flag)
      if (flag == -1) {
        break
      }
      val todoStr = responseData.substring(flag)
          .toUpperCase() //注意大小写
      //获取下个包的长度
      val package_length = todoStr.substring(8, 12)
          .toInt(16)
      val packageStr = todoStr.substring(0, (14 + package_length) * 2)
      flag += (14 + package_length) * 2
      val socketPackage = SocketPackage()
      socketPackage.packageHead = DeviceUtil.getTerminalID()
      socketPackage.packageLength = packageStr.substring(8, 12)
      socketPackage.packageType = packageStr.substring(12, 14)
      socketPackage.packageSN = packageStr.substring(14, 28)
      socketPackage.packageContent = packageStr.substring(28)
      packageList.add(socketPackage)
    }
    //遍历，分发
    while (!packageList.isEmpty()) {
      val socketPackage: SocketPackage = packageList.poll() ?: break
      when (socketPackage.packageType!!.toInt(16)) {
        18 -> RegisterResolver.handlePackage(socketPackage)
        4 -> HeartBeatResolver.handlePackage(socketPackage)
        35 -> {
          val subType2 = socketPackage.packageContent!!.substring(0, 2)
              .toInt(16) //子命令
          when (subType2) {
            0 -> ShellResolver.handlePackage(socketPackage)
//            1 -> UpdateAPKResolver.handlePackage(socketPackage) 已废弃
            2 -> LogUploadResolver.handlePackage(socketPackage)
//            3 -> GetDeviceInfoResolver.handlePackage(socketPackage) 已废弃
            else -> SocketLoggerUtil.logSocket(
                "未知的包类型Monitor-子命令$subType2",
                LoggerUtil.LOGGERUTIL_ERROR
            )
          }
        }
        25 -> {
          val subType = socketPackage.packageContent!!.substring(0, 2)
              .toInt(16) //子命令
          when (subType) {
            //10.10 S向MB转发下载广告文件的命令 0x02(下载安装包)
            2 -> DownloadFileResolver.handlePackage(socketPackage)
            else -> SocketLoggerUtil.logSocket("未知的包类型远程更新-子命令$subType", LoggerUtil.LOGGERUTIL_ERROR)
          }
        }
        else -> SocketLoggerUtil.logSocket(
            "未知的包类型" + socketPackage.packageType, LoggerUtil.LOGGERUTIL_ERROR
        )
      }
    }
  }
}